/**
 * 
 */
package com.cp16.tss.form;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import com.cp16.tss.handler.CustomerHandler;

/**
 * @author GiangHN
 * 
 */
public class CustomerForm extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTable tblResultSearch;
	private JPanel panAdd;
	private JLabel lblListForCustomer;
	private JPanel panResult;
	private JButton btnClose;
	private JButton btnEdit;
	private JButton btnDelete;
	private JLabel lblId;
	private JLabel label_1;
	private JTextField txtCustomerId;
	private JLabel lblName;
	private JLabel label_3;
	private JTextField txtCustomerName;
	private JLabel lblStreet;
	private JTextField txtStreet;
	private JLabel lblDistrict;
	private JTextField txtDistrict;
	private JTextField txtCity;
	private JTextField txtPhone;
	private JLabel lblCity;
	private JLabel lblPhone;
	private JButton btnAdd;
	private JButton btnRefresh;
	private JButton btnOk;
	private JButton btnCancel;
	private JLabel lblTotalCustomer;
	private JButton btnClear;
	private JLabel lblNameSearch;
	private JTextField txtNameSearch;
	private JButton btnSearch;
	private JLabel lblIdSearch;
	private JTextField txtIdSearch;
	private JScrollPane scrollResult;


	/**
	 * Create the frame.
	 */

	public CustomerForm() {
		loadLookAndFeel();
		initComponents();
	}

	private void loadLookAndFeel() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException ex) {
			Logger.getLogger(CustomerForm.class.getName()).log(Level.SEVERE,
					null, ex);
		} catch (InstantiationException ex) {
			Logger.getLogger(CustomerForm.class.getName()).log(Level.SEVERE,
					null, ex);
		} catch (IllegalAccessException ex) {
			Logger.getLogger(CustomerForm.class.getName()).log(Level.SEVERE,
					null, ex);
		} catch (UnsupportedLookAndFeelException ex) {
			Logger.getLogger(CustomerForm.class.getName()).log(Level.SEVERE,
					null, ex);
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void initComponents() {

		setTitle("CustomerCare- created by Hoang Ngoc Giang");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setLocation(150,70);
		setSize(980, 650);
		setResizable(false);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(102, 102, 204));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		panAdd = new JPanel();
		panAdd.setBounds(10, 11, 954, 140);
		contentPane.add(panAdd);
		panAdd.setLayout(null);

		lblListForCustomer = new JLabel("Add new Customer");
		lblListForCustomer.setBounds(373, 11, 291, 30);
		lblListForCustomer.setFont(new Font("Tahoma", Font.PLAIN, 22));
		panAdd.add(lblListForCustomer);
		
		txtCustomerId = new JTextField();
		txtCustomerId.setBounds(86, 52, 94, 29);
		txtCustomerId.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtCustomerId.setColumns(10);
		panAdd.add(txtCustomerId);
		
		lblId = new JLabel("ID");
		lblId.setBounds(21, 52, 27, 29);
		lblId.setFont(new Font("Tahoma", Font.PLAIN, 15));
		panAdd.add(lblId);
		
		label_1 = new JLabel("*");
		label_1.setBounds(71, 52, 14, 29);
		label_1.setForeground(Color.RED);
		label_1.setFont(new Font("Tahoma", Font.PLAIN, 15));
		panAdd.add(label_1);

		
		lblName = new JLabel("Name");
		lblName.setBounds(301, 52, 49, 29);
		lblName.setFont(new Font("Tahoma", Font.PLAIN, 15));
		panAdd.add(lblName);
		
		label_3 = new JLabel("*");
		label_3.setBounds(361, 52, 14, 29);
		label_3.setForeground(Color.RED);
		label_3.setFont(new Font("Tahoma", Font.PLAIN, 15));
		panAdd.add(label_3);
		
		txtCustomerName = new JTextField();
		txtCustomerName.setBounds(373, 52, 199, 29);
		txtCustomerName.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtCustomerName.setColumns(10);
		panAdd.add(txtCustomerName);
		
		lblStreet = new JLabel("Street");
		lblStreet.setBounds(21, 91, 57, 29);
		lblStreet.setFont(new Font("Tahoma", Font.PLAIN, 15));
		panAdd.add(lblStreet);
		
		txtStreet = new JTextField();
		txtStreet.setBounds(86, 91, 199, 29);
		txtStreet.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtStreet.setColumns(10);
		panAdd.add(txtStreet);
		
		lblDistrict = new JLabel("District");
		lblDistrict.setBounds(301, 92, 55, 29);
		lblDistrict.setFont(new Font("Tahoma", Font.PLAIN, 15));
		panAdd.add(lblDistrict);
		
		txtDistrict = new JTextField();
		txtDistrict.setBounds(373, 92, 199, 29);
		txtDistrict.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtDistrict.setColumns(10);
		panAdd.add(txtDistrict);
		
		txtCity = new JTextField();
		txtCity.setBounds(648, 92, 199, 29);
		txtCity.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtCity.setColumns(10);
		panAdd.add(txtCity);
		
		lblCity = new JLabel("City");
		lblCity.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblCity.setBounds(592, 91, 57, 29);
		panAdd.add(lblCity);
		
		lblPhone = new JLabel("Phone");
		lblPhone.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblPhone.setBounds(592, 52, 57, 29);
		panAdd.add(lblPhone);
		
		txtPhone = new JTextField();
		txtPhone.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtPhone.setColumns(10);
		txtPhone.setBounds(648, 52, 103, 29);
		panAdd.add(txtPhone);

		panResult = new JPanel();
		panResult.setBounds(10, 162, 954, 449);
		contentPane.add(panResult);
		panResult.setLayout(null);
		
		scrollResult = new JScrollPane();
		scrollResult.setBounds(10, 11, 934, 386);
		panResult.add(scrollResult);

		Vector rowHeader = new Vector();
		rowHeader.add("No");
		rowHeader.add("ID");
		rowHeader.add("Name");
		rowHeader.add("Street name");
		rowHeader.add("District");
		rowHeader.add("City");
		rowHeader.add("Phone");
		DefaultTableModel defaultTableModel = new DefaultTableModel(rowHeader, 0);
		
		tblResultSearch = new JTable();
		tblResultSearch.setBounds(10, 11, 934, 386);
		tblResultSearch.setFont(new Font("Tahoma", Font.PLAIN, 13));
		tblResultSearch.setModel(defaultTableModel);
		
		tblResultSearch.getColumnModel().getColumn(0).setPreferredWidth(70);
		tblResultSearch.getColumnModel().getColumn(1).setPreferredWidth(70);
		tblResultSearch.getColumnModel().getColumn(2).setPreferredWidth(180);
		tblResultSearch.getColumnModel().getColumn(3).setPreferredWidth(180);
		tblResultSearch.getColumnModel().getColumn(4).setPreferredWidth(100);
		tblResultSearch.getColumnModel().getColumn(5).setPreferredWidth(100);
		
		scrollResult.setViewportView(tblResultSearch);
		
		lblTotalCustomer = new JLabel("");
		lblTotalCustomer.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblTotalCustomer.setBounds(780, 408, 179, 29);
		panResult.add(lblTotalCustomer);
		
		btnAdd = new JButton("Add");
		btnAdd.setBounds(857, 91, 77, 29);
		btnAdd.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnAdd.addActionListener(this);
		panAdd.add(btnAdd);
		
		btnOk = new JButton("OK");
		btnOk.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnOk.setBounds(857, 51, 77, 29);
		btnOk.setVisible(false);
		btnOk.addActionListener(this);
		panAdd.add(btnOk);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnCancel.setBounds(857, 91, 77, 29);
		btnCancel.setVisible(false);
		btnCancel.addActionListener(this);
		panAdd.add(btnCancel);
		
		btnClear = new JButton("Clear");
		btnClear.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnClear.setBounds(857, 51, 77, 29);
		btnClear.addActionListener(this);
		panAdd.add(btnClear);
		
		btnRefresh = new JButton("Refresh");
		btnRefresh.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnRefresh.setBackground(new Color(230, 230, 250));
		btnRefresh.setBounds(385, 407, 83, 30);
		btnRefresh.addActionListener(this);
		panResult.add(btnRefresh);

		btnClose = new JButton("Close");
		btnClose.setBounds(635, 407, 83, 30);
		btnClose.setBackground(new Color(230, 230, 250));
		btnClose.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnClose.addActionListener(this);
		panResult.add(btnClose);

		btnEdit = new JButton("Edit");
		btnEdit.setBounds(468, 407, 83, 30);
		btnEdit.setBackground(new Color(230, 230, 250));
		btnEdit.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnEdit.addActionListener(this);
		panResult.add(btnEdit);

		btnDelete = new JButton("Delete");
		btnDelete.setBounds(551, 407, 83, 30);
		btnDelete.setBackground(new Color(230, 230, 250));
		btnDelete.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnDelete.addActionListener(this);
		panResult.add(btnDelete);
		
		lblNameSearch = new JLabel("Name");
		lblNameSearch.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblNameSearch.setBounds(88, 408, 45, 29);
		panResult.add(lblNameSearch);
		
		txtNameSearch = new JTextField();
		txtNameSearch.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtNameSearch.setColumns(10);
		txtNameSearch.setBounds(138, 408, 122, 29);
		panResult.add(txtNameSearch);
		
		btnSearch = new JButton("Search");
		btnSearch.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnSearch.setBackground(new Color(230, 230, 250));
		btnSearch.setBounds(262, 407, 83, 30);
		btnSearch.addActionListener(this);
		panResult.add(btnSearch);
		
		lblIdSearch = new JLabel("ID");
		lblIdSearch.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblIdSearch.setBounds(10, 408, 27, 29);
		panResult.add(lblIdSearch);
		
		txtIdSearch = new JTextField();
		txtIdSearch.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtIdSearch.setColumns(10);
		txtIdSearch.setBounds(33, 408, 45, 29);
		panResult.add(txtIdSearch);
	}


	/**
	 * @return the tblResultSearch
	 */
	public JTable getTblResultSearch() {
		return tblResultSearch;
	}

	/**
	 * @param tblResultSearch the tblResultSearch to set
	 */
	public void setTblResultSearch(JTable tblResultSearch) {
		this.tblResultSearch = tblResultSearch;
	}

	/**
	 * @return the lblListForCustomer
	 */
	public JLabel getLblListForCustomer() {
		return lblListForCustomer;
	}

	/**
	 * @param lblListForCustomer the lblListForCustomer to set
	 */
	public void setLblListForCustomer(JLabel lblListForCustomer) {
		this.lblListForCustomer = lblListForCustomer;
	}

	/**
	 * @return the btnClose
	 */
	public JButton getBtnClose() {
		return btnClose;
	}

	/**
	 * @param btnClose the btnClose to set
	 */
	public void setBtnClose(JButton btnClose) {
		this.btnClose = btnClose;
	}

	/**
	 * @return the btnEdit
	 */
	public JButton getBtnEdit() {
		return btnEdit;
	}

	/**
	 * @param btnEdit the btnEdit to set
	 */
	public void setBtnEdit(JButton btnEdit) {
		this.btnEdit = btnEdit;
	}

	/**
	 * @return the btnDelete
	 */
	public JButton getBtnDelete() {
		return btnDelete;
	}

	/**
	 * @param btnDelete the btnDelete to set
	 */
	public void setBtnDelete(JButton btnDelete) {
		this.btnDelete = btnDelete;
	}

	/**
	 * @return the lblId
	 */
	public JLabel getLblId() {
		return lblId;
	}

	/**
	 * @param lblId the lblId to set
	 */
	public void setLblId(JLabel lblId) {
		this.lblId = lblId;
	}

	/**
	 * @return the label_1
	 */
	public JLabel getLabel_1() {
		return label_1;
	}

	/**
	 * @param label_1 the label_1 to set
	 */
	public void setLabel_1(JLabel label_1) {
		this.label_1 = label_1;
	}

	/**
	 * @return the txtCustomerId
	 */
	public JTextField getTxtCustomerId() {
		return txtCustomerId;
	}

	/**
	 * @param txtCustomerId the txtCustomerId to set
	 */
	public void setTxtCustomerId(JTextField txtCustomerId) {
		this.txtCustomerId = txtCustomerId;
	}

	/**
	 * @return the lblName
	 */
	public JLabel getLblName() {
		return lblName;
	}

	/**
	 * @param lblName the lblName to set
	 */
	public void setLblName(JLabel lblName) {
		this.lblName = lblName;
	}

	/**
	 * @return the label_3
	 */
	public JLabel getLabel_3() {
		return label_3;
	}

	/**
	 * @param label_3 the label_3 to set
	 */
	public void setLabel_3(JLabel label_3) {
		this.label_3 = label_3;
	}

	/**
	 * @return the txtCustomerName
	 */
	public JTextField getTxtCustomerName() {
		return txtCustomerName;
	}

	/**
	 * @param txtCustomerName the txtCustomerName to set
	 */
	public void setTxtCustomerName(JTextField txtCustomerName) {
		this.txtCustomerName = txtCustomerName;
	}

	/**
	 * @return the lblStreet
	 */
	public JLabel getLblStreet() {
		return lblStreet;
	}

	/**
	 * @param lblStreet the lblStreet to set
	 */
	public void setLblStreet(JLabel lblStreet) {
		this.lblStreet = lblStreet;
	}

	/**
	 * @return the txtStreet
	 */
	public JTextField getTxtStreet() {
		return txtStreet;
	}

	/**
	 * @param txtStreet the txtStreet to set
	 */
	public void setTxtStreet(JTextField txtStreet) {
		this.txtStreet = txtStreet;
	}

	/**
	 * @return the lblDistrict
	 */
	public JLabel getLblDistrict() {
		return lblDistrict;
	}

	/**
	 * @param lblDistrict the lblDistrict to set
	 */
	public void setLblDistrict(JLabel lblDistrict) {
		this.lblDistrict = lblDistrict;
	}

	/**
	 * @return the txtDistrict
	 */
	public JTextField getTxtDistrict() {
		return txtDistrict;
	}

	/**
	 * @param txtDistrict the txtDistrict to set
	 */
	public void setTxtDistrict(JTextField txtDistrict) {
		this.txtDistrict = txtDistrict;
	}

	/**
	 * @return the txtCity
	 */
	public JTextField getTxtCity() {
		return txtCity;
	}

	/**
	 * @param txtCity the txtCity to set
	 */
	public void setTxtCity(JTextField txtCity) {
		this.txtCity = txtCity;
	}

	/**
	 * @return the txtPhone
	 */
	public JTextField getTxtPhone() {
		return txtPhone;
	}

	/**
	 * @param txtPhone the txtPhone to set
	 */
	public void setTxtPhone(JTextField txtPhone) {
		this.txtPhone = txtPhone;
	}

	/**
	 * @return the lblCity
	 */
	public JLabel getLblCity() {
		return lblCity;
	}

	/**
	 * @param lblCity the lblCity to set
	 */
	public void setLblCity(JLabel lblCity) {
		this.lblCity = lblCity;
	}

	/**
	 * @return the lblPhone
	 */
	public JLabel getLblPhone() {
		return lblPhone;
	}

	/**
	 * @param lblPhone the lblPhone to set
	 */
	public void setLblPhone(JLabel lblPhone) {
		this.lblPhone = lblPhone;
	}

	/**
	 * @return the btnAdd
	 */
	public JButton getBtnAdd() {
		return btnAdd;
	}

	/**
	 * @param btnAdd the btnAdd to set
	 */
	public void setBtnAdd(JButton btnAdd) {
		this.btnAdd = btnAdd;
	}

	/**
	 * @return the btnRefresh
	 */
	public JButton getBtnRefresh() {
		return btnRefresh;
	}

	/**
	 * @param btnRefresh the btnRefresh to set
	 */
	public void setBtnRefresh(JButton btnRefresh) {
		this.btnRefresh = btnRefresh;
	}

	/**
	 * @return the btnOk
	 */
	public JButton getBtnOk() {
		return btnOk;
	}

	/**
	 * @param btnOk the btnOk to set
	 */
	public void setBtnOk(JButton btnOk) {
		this.btnOk = btnOk;
	}

	/**
	 * @return the btnCancel
	 */
	public JButton getBtnCancel() {
		return btnCancel;
	}

	/**
	 * @param btnCancel the btnCancel to set
	 */
	public void setBtnCancel(JButton btnCancel) {
		this.btnCancel = btnCancel;
	}

	/**
	 * @return the lblTotalCustomer
	 */
	public JLabel getLblTotalCustomer() {
		return lblTotalCustomer;
	}

	/**
	 * @param lblTotalCustomer the lblTotalCustomer to set
	 */
	public void setLblTotalCustomer(JLabel lblTotalCustomer) {
		this.lblTotalCustomer = lblTotalCustomer;
	}

	/**
	 * @return the btnClear
	 */
	public JButton getBtnClear() {
		return btnClear;
	}

	/**
	 * @param btnClear the btnClear to set
	 */
	public void setBtnClear(JButton btnClear) {
		this.btnClear = btnClear;
	}

	/**
	 * @return the lblNameSearch
	 */
	public JLabel getLblNameSearch() {
		return lblNameSearch;
	}

	/**
	 * @param lblNameSearch the lblNameSearch to set
	 */
	public void setLblNameSearch(JLabel lblNameSearch) {
		this.lblNameSearch = lblNameSearch;
	}

	/**
	 * @return the txtNameSearch
	 */
	public JTextField getTxtNameSearch() {
		return txtNameSearch;
	}

	/**
	 * @param txtNameSearch the txtNameSearch to set
	 */
	public void setTxtNameSearch(JTextField txtNameSearch) {
		this.txtNameSearch = txtNameSearch;
	}

	/**
	 * @return the btnSearch
	 */
	public JButton getBtnSearch() {
		return btnSearch;
	}

	/**
	 * @param btnSearch the btnSearch to set
	 */
	public void setBtnSearch(JButton btnSearch) {
		this.btnSearch = btnSearch;
	}

	/**
	 * @return the lblIdSearch
	 */
	public JLabel getLblIdSearch() {
		return lblIdSearch;
	}

	/**
	 * @param lblIdSearch the lblIdSearch to set
	 */
	public void setLblIdSearch(JLabel lblIdSearch) {
		this.lblIdSearch = lblIdSearch;
	}

	/**
	 * @return the txtIdSearch
	 */
	public JTextField getTxtIdSearch() {
		return txtIdSearch;
	}

	/**
	 * @param txtIdSearch the txtIdSearch to set
	 */
	public void setTxtIdSearch(JTextField txtIdSearch) {
		this.txtIdSearch = txtIdSearch;
	}
	
	/**
	 * @return the scrollResult
	 */
	public JScrollPane getScrollResult() {
		return scrollResult;
	}

	/**
	 * @param scrollResult the scrollResult to set
	 */
	public void setScrollResult(JScrollPane scrollResult) {
		this.scrollResult = scrollResult;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		CustomerHandler customerHandler = new CustomerHandler();
		// search
		if (e.getSource() == btnRefresh) {
			customerHandler.loadTable(this);
		// add
		} else if (e.getSource() == btnAdd) {
			customerHandler.addNewCustomer(this);
		// edit
		} else if (e.getSource() == btnEdit) {
			customerHandler.editCustomerForm(this);
		// delete
		} else if (e.getSource() == btnDelete){
			customerHandler.deleteCustomerFromTable(this);
		// OK edit
		} else if (e.getSource() == btnOk){
			customerHandler.editCustomer(this);
		// clear add form
		} else if (e.getSource() == btnSearch){
			customerHandler.searchCustomer(this);
		} else if (e.getSource() == btnClear || e.getSource() == btnCancel){
			customerHandler.doClearAddNewCustomerForm(this);
			customerHandler.enableButtonWhenEditCustomer(this);
		} else if(e.getSource() == btnClose){
			System.exit(0);
		}
	}
}
